<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class DERecordationAndTransferTaxCalculator{

    private $rateInfoProperties;
    private $titleInsuranceCalculatorHelper;
    private $state;
    private $countyLocales;
    private $taxCalculatorHelper;
    private $feeCalculator;

    public function DERecordationAndTransferTaxCalculator($state, $rateInfoProperties) 
    {
       $this->state = $state;
       $this->rateInfoProperties = $rateInfoProperties;
       
       $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
       
       $this->taxCalculatorHelper = new TaxCalculatorHelper();
       
       $this->countyLocales = CountyLocale::getCountyLocales($state);
       $this->feeCalculator = new FeeCalculator($state, $rateInfoProperties);
    }    

    public function supportsCountyLocale() {
        return true;
    }

    public function getCountyLocales() {
        return $this->countyLocales;
    }

    public function supportsTownLocale() {
        return false;
    }

    public function getTownLocales($countyLocale) {
        return array();
    }
    public function calculatePurchaseRecordationAndTransferTax($countyLocale,$townLocale,$consideration,$loanAmounts,$qualifiedFirstTimeBuyer,$ownerOccupiedResidence,$requestedPayer) 
    {

        if ($townLocale == null) {
            return null;
        }
        if ($countyLocale == null) {
            return null;
        }
        //echo $consideration; exit();

        $propertyTransferTax = $this->getStatePropertyTransferTax($countyLocale, $townLocale, $consideration, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence);
        $countyPropertyTransferTax = $this->getCountyTransferTax($countyLocale, $townLocale, $consideration, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence);        
        //print_r($propertyTransferTax); exit();
        //print_r($countyPropertyTransferTax); exit();
        
        // zero indicates false
        $mortgage = $this->taxCalculatorHelper->getNumberOfLoans($loanAmounts);
        //echo $mortgage; exit();

        // CT-32 Added fees per State
        $fees = $this->feeCalculator->calculate(true, $mortgage, $ownerOccupiedResidence);
        //print_r($fees); exit();

        return RecordationAndTransferTaxCreator::createSplitRecordationAndTransferTaxes($this->state,
                $propertyTransferTax,
                $countyPropertyTransferTax,
                null,   // CT-26 this is not a separate tax in DE
                0.0,
                $fees,
                $requestedPayer, null);
    }

    public function getHeaderHTML() {
        return "Delaware and the municipality where the property is located assess transfer taxes on a purchase/sale of real property, but not on refinance transactions. However, there are recording fees, as shown below, which are collected on both purchase and refinance transactions.";
    }

    public function getFooterHTML() {
        return null;
    }

    public function calculateRefinanceRecordationAndTransferTax($countyLocale, $loanAmounts, $ownerOccupiedResidence,$payoffAmounts,$sameLender) {
        // Adam is quite sure there is no refinance tax
        return new RecordationAndTransferTaxes(
                null, null, null,
                $this->feeCalculator->calculate(false, count($loanAmounts), $ownerOccupiedResidence),
                null);
    }

    // Arden, New Castle County DE
    // TRANSFER AND FINANCING FEES/TAXES:
    // Realty Transfer Tax	.................... $0.03 times value of property
    // Municipal transfer tax in the amount of 1.5% of the fair market value is due in many municipalities in addition
    // to the state transfer tax of 1.5% of the fair market value. Where the municipality does not collect a tax,
    // however, the state transfer tax is 2%. Please see State Summary for details.

    private function getCountyTransferTax($countyLocale, $townLocale, $consideration, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence) {
        $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.de.rate");//0.015;
        //echo $tax; exit();

        if ($countyLocale->equals(CountyLocale::Kent)) {
            if (!$this->hasTownTax($countyLocale, $townLocale)) {
                //echo "ok"; exit();
                $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.de.kent.hastowntax.rate");//0.0;
            }

            // If first time home buyer these towns give an exemption.
            if ($qualifiedFirstTimeBuyer != null && $qualifiedFirstTimeBuyer->getValue()) {
                //echo "ok"; exit();
                if ($townLocale->equals(TownLocale::Dover) || $townLocale->equals(TownLocale::Milford) || $townLocale->equals(TownLocale::Felton)) {
                    $tax -= $this->rateInfoProperties->getDoubleValue("recordation.county.de.kent.firsttimebuyer.discount");//0.015;
                }
            }
        } else if ($countyLocale->equals(CountyLocale::New_Castle)) {
            if ($townLocale->equals(TownLocale::Arden) || $townLocale->equals(TownLocale::Bellefonte) || $townLocale->equals(TownLocale::Ardentown) || $townLocale->equals(TownLocale::Ardencroft)) {
                $tax -= $this->rateInfoProperties->getDoubleValue("recordation.county.de.newcastle.town.discount");//0.015;
            } else if ($townLocale->equals(TownLocale::All_Others) && $qualifiedFirstTimeBuyer != null && $qualifiedFirstTimeBuyer->getValue() && $ownerOccupiedResidence != null && $ownerOccupiedResidence->getValue()) {
                // New Castle County collects 0.75% real estate transfer tax where the homebuyer asre first time purchasers and will use the property as a principal residence.
                $tax = $this->rateInfoProperties->getDoubleValue("recordation.county.de.newcastle.firsttimebuyer.rate");//0.0075;
            }
        } else if ($countyLocale->equals(CountyLocale::Sussex)) {
            if ($qualifiedFirstTimeBuyer != null && $qualifiedFirstTimeBuyer->getValue()) {
                if ($townLocale->equals(TownLocale::Ellendale) || $townLocale->equals(TownLocale::Georgetown) || $townLocale->equals(TownLocale::Greenwood) || $townLocale->equals(TownLocale::Laurel) || $townLocale->equals(TownLocale::Lincoln) || $townLocale->equals(TownLocale::Milford) || $townLocale->equals(TownLocale::Prime_Hook_Beach) || $townLocale->equals(TownLocale::Seaford)) {
                    // If first time home buyer these towns give an exemption.
                    $tax -= $this->rateInfoProperties->getDoubleValue("recordation.county.de.sussex.firsttimebuyer.discount.1");//0.015;
                } else if ($townLocale->equals(TownLocale::Delmar)) {
                    $tax -= $this->rateInfoProperties->getDoubleValue("recordation.county.de.sussex.firsttimebuyer.discount.2");//0.005;
                }
            }
        }

        return new CountyPropertyTransferTax("Municipal Transfer Tax", $consideration * $tax);
    }

    private function hasTownTax($countyLocale, $townLocale) {
        //noinspection SimplifiableIfStatement
        if ($countyLocale->equals(CountyLocale::Kent)) {
            // These towns have no local tax
            return !($townLocale->equals(TownLocale::Farmington) || $townLocale->equals(TownLocale::Hartly) || $townLocale->equals(TownLocale::Little_Creek) || $townLocale->equals(TownLocale::Woodside));
        } else if ($countyLocale->equals(CountyLocale::New_Castle)) {
            // These towns have no local tax
            return !($townLocale->equals(TownLocale::Arden) || $townLocale->equals(TownLocale::Bellefonte) || $townLocale->equals(TownLocale::Ardentown) || $townLocale->equals(TownLocale::Ardencroft));
        }
        return true;
    }

    private function getStatePropertyTransferTax($countyLocale, $townLocale, $consideration, $qualifiedFirstTimeBuyer, $ownerOccupiedResidence) {
        if ($this->hasTownTax($countyLocale, $townLocale)) {
            return new StatePropertyTransferTax("State Transfer Tax", $consideration * $this->rateInfoProperties->getDoubleValue("recordation.state.de.hastowntax.rate"));
        } else {
            return new StatePropertyTransferTax("State Transfer Tax", $consideration * $this->rateInfoProperties->getDoubleValue("recordation.state.de.rate"));
        }
    }
}
?>
